package com.xjtu.project.controller.admin;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.xjtu.project.common.utls.JsonResponse;
import com.xjtu.project.model.domain.Shoe;
import com.xjtu.project.model.domain.Size;
import com.xjtu.project.model.dto.ShoeDTO;
import com.xjtu.project.model.dto.ShoeIfDTO;
import com.xjtu.project.model.vo.ShoeVO;
import com.xjtu.project.service.ShoeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/admin")
public class ShoeController {

    @Autowired
    private ShoeService shoeService;

    @PutMapping("shoes")
    public JsonResponse updateShoe(@RequestBody Shoe shoe) {
        boolean b = shoeService.updateByIdMy(shoe);
        return JsonResponse.success(b);
    }

    @PostMapping("shoes")
    public JsonResponse addShoe(@RequestBody ShoeDTO shoe) {
        boolean b = shoeService.addShoeMy(shoe);
        return JsonResponse.success(b);
    }

    @GetMapping("shoe/{id}")
    public JsonResponse getShoe(@PathVariable("id") Long id) {
        ShoeVO shoe = shoeService.getByIdMy(id);
        return JsonResponse.success(shoe);
    }

    @GetMapping("shoe/list")
    public JsonResponse getShoeList(ShoeIfDTO shoe) {
        Page<Shoe> lists = shoeService.selectByCategoryId(shoe);
        return JsonResponse.success(lists);
    }

    @PostMapping("shoes/status/{status}")
    public JsonResponse getShoeStatus(@PathVariable("status") Integer status,Integer id) {
        boolean b = shoeService.updateByStatus(status,id);
        return JsonResponse.success(b);
    }

    @GetMapping("shoes/page")
    public JsonResponse getShoePage(ShoeIfDTO shoe) {
        Page<Shoe> lists = shoeService.selectPagedMy(shoe);
        return JsonResponse.success(lists);
    }

    @DeleteMapping("shoes")
    public JsonResponse deleteShoe(@RequestBody List<Integer> ids){
        boolean b = shoeService.removeByIds(ids);
        Db.lambdaUpdate(Size.class).in(!CollectionUtils.isEmpty(ids),Size::getShoeId, ids).remove();
        return JsonResponse.success(b);
    }
}
